package hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities;

import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.activity.BaseActivity;
import hczc.longcai.com.hccrowdfunding.activity.beans.EditPass;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.utils.httputils.HttpPath;
import hczc.longcai.com.hccrowdfunding.utils.httputils.LoadDatas;
import hczc.longcai.com.hccrowdfunding.utils.json.ParseJSON;

public class AlterPasswdActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "AlterPasswdActivity";
    private EditText oldPassword;
    private EditText newPassword;
    private EditText affirmPassword;
    private Button button;
    private SharedPreferences mSharedPreferences;
    private String uid;
    private LoadDatas loadDatas;

    private EditPassAsyncTask asyncTask;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter_passwd);

        mSharedPreferences = BaseApplication.mSharedPreferences;
        loadDatas = new LoadDatas();
        initDatas();

        initViews();
    }

    private void initDatas() {
        uid = mSharedPreferences.getString("uid","");
    }

    private void initViews() {
        findViewById(R.id.head_back).setOnClickListener(this);
        ((TextView) findViewById(R.id.head_title)).setText("修改密码");

        oldPassword = ((EditText) findViewById(R.id.alter_passwd_old));
        newPassword = ((EditText) findViewById(R.id.alter_passwd_new));
        affirmPassword = ((EditText) findViewById(R.id.alter_passwd_affirm));

        button = ((Button) findViewById(R.id.alter_passwd_button));
        button.setOnClickListener(this);

    }

    /**
     * 设置EditText的焦点
     *
     * @param et 要设置的EditText
     */
    private void setFocus(EditText et) {
        et.setFocusable(true);
        et.setFocusableInTouchMode(true);
        et.requestFocus();
        et.findFocus();
    }

    private boolean judgePassword(String old_p,String new_p,String affirm_p) {

        boolean flag = true;
        if (TextUtils.isEmpty(old_p)) {
            Toast.makeText(AlterPasswdActivity.this, "请输入旧密码", Toast.LENGTH_SHORT).show();
            setFocus(oldPassword);
            flag = false;
            return flag;
        }

        if (TextUtils.isEmpty(new_p)) {
            Toast.makeText(AlterPasswdActivity.this, "请输入新密码", Toast.LENGTH_SHORT).show();
            setFocus(newPassword);
            flag = false;
            return flag;
        } else if (new_p.length() < 6) {
            Toast.makeText(AlterPasswdActivity.this, "请输入大于6位的密码", Toast.LENGTH_SHORT).show();
            newPassword.setText("");
            affirmPassword.setText("");
            setFocus(newPassword);
            flag = false;
            return flag;
        } else if (new_p.length() > 20) {
            Toast.makeText(AlterPasswdActivity.this, "请输入小于20位的密码", Toast.LENGTH_SHORT).show();
            newPassword.setText("");
            affirmPassword.setText("");
            setFocus(newPassword);
            flag = false;
            return flag;
        }

        if (TextUtils.isEmpty(affirm_p)) {
            Toast.makeText(AlterPasswdActivity.this, "请输入确认密码", Toast.LENGTH_SHORT).show();
            setFocus(affirmPassword);
            flag = false;
            return flag;
        } else if (!new_p.equals(affirm_p)) {
            Toast.makeText(AlterPasswdActivity.this, "两次输入的密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
            newPassword.setText("");
            affirmPassword.setText("");
            setFocus(affirmPassword);
            flag = false;
            return flag;
        }

        return flag;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.head_back:
                finish();
                break;
            case R.id.alter_passwd_button:
                isSuccess();
                break;
        }
    }

    private void isSuccess() {
        String old_p = oldPassword.getText().toString();
        String new_p = newPassword.getText().toString();
        String affirm_p = affirmPassword.getText().toString();
        if (judgePassword(old_p,new_p,affirm_p)) {
            asyncTask = new EditPassAsyncTask(uid,old_p,new_p,affirm_p);
            asyncTask.execute(((Void) null));
//            Toast.makeText(AlterPasswdActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
//            finish();
        }
    }

    class EditPassAsyncTask extends AsyncTask<Void,Void,Boolean>{

        private String tips;

        private String uid;
        private String oldPasswd;
        private String newPasswd;
        private String newPasswdRe;

        public EditPassAsyncTask(String uid, String oldPasswd, String newPasswd, String newPasswdRe) {
            this.uid = uid;
            this.oldPasswd = oldPasswd;
            this.newPasswd = newPasswd;
            this.newPasswdRe = newPasswdRe;
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            boolean flag = false;
            try {
                String url_editpass = HttpPath.getURL_Editpass(uid, oldPasswd, newPasswd, newPasswdRe);
                Log.d(TAG, url_editpass);
                byte[] datas = loadDatas.getDatas(url_editpass);
                String JSON = new String(datas, 0, datas.length);
                EditPass editPass = ParseJSON.parseEdit(JSON);
                String status = editPass.getStatus();
                if ("1".equals(status)){
                    flag = true;
                }else {
                    tips = editPass.getTips();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return flag;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            if (aBoolean){
                Toast.makeText(AlterPasswdActivity.this,"修改成功！",Toast.LENGTH_SHORT).show();
                finish();
            }else {
                Toast.makeText(AlterPasswdActivity.this,tips,Toast.LENGTH_SHORT).show();
            }
        }
    }
}
